package com.softeem.god.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.god.comm.R;
import com.softeem.god.entity.User;
import com.softeem.god.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "用户管理", description = "用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户列表", description = "根据提供的页码和数据条数实现分页查询")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "数据条数"),
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "phone", description = "手机号")
    })
    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public R list(@RequestParam(defaultValue = "1") Integer page, 
                  @RequestParam(defaultValue = "5") Integer limit,
                  @RequestParam(required = false) String username,
                  @RequestParam(required = false) String phone) {
        //创建分页对象
        IPage<User> ipage = new Page<>(page, limit);
        
        // 创建查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            queryWrapper.like("username", username);
        }
        if (phone != null && !phone.isEmpty()) {
            queryWrapper.like("phone", phone);
        }
        
        //执行查询(分页)
        IPage<User> result = userService.page(ipage, queryWrapper);
        Long count = userService.count(queryWrapper);
        //返回结果
        return R.success().setData(result).setCount( count);
    }

    @Operation(summary = "根据ID获取用户信息", description = "根据用户ID获取用户详细信息")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @GetMapping(value = "/info", produces = "application/json;charset=UTF-8")
    public R info(@RequestParam Integer id) {
        User user = userService.getById(id);
        return user != null ? R.success().setData(user) : R.error().setMsg("用户不存在");
    }

    @Operation(summary = "添加用户", description = "添加新用户")
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    public R add(@RequestBody User user) {
        // 设置创建时间
        user.setCreateTime(new Date());
        // 设置默认状态为正常
        user.setStatus(1);
        boolean result = userService.save(user);
        return result ? R.success().setMsg("添加成功") : R.error().setMsg("添加失败");
    }

    @Operation(summary = "更新用户", description = "根据ID更新用户信息")
    @PutMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public R update(@RequestBody User user) {
        boolean result = userService.updateById(user);
        return result ? R.success().setMsg("更新成功") : R.error().setMsg("更新失败");
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @Parameters({
            @Parameter(name = "id", description = "用户ID")
    })
    @DeleteMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public R delete(@RequestParam Integer id) {
        boolean result = userService.removeById(id);
        return result ? R.success().setMsg("删除成功") : R.error().setMsg("删除失败");
    }

    @Operation(summary = "更新用户状态", description = "启用或禁用用户")
    @Parameters({
            @Parameter(name = "id", description = "用户ID"),
            @Parameter(name = "status", description = "状态(0-禁用,1-正常)")
    })
    @PutMapping(value = "/status", produces = "application/json;charset=UTF-8")
    public R updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        boolean result = userService.updateById(user);
        return result ? R.success().setMsg("状态更新成功") : R.error().setMsg("状态更新失败");
    }

    @Operation(summary = "用户登录", description = "用户登录")
    @GetMapping(value = "/login", produces = "application/json;charset=UTF-8")
    public R login(String username, String password) {
        return userService.login(username, password);
    }

    @Operation(summary = "用户注册", description = "用户注册")
    @PostMapping(value = "/reg", produces = "application/json;charset=UTF-8")
    public R register(@RequestBody User user) {
        return userService.register(user);
    }
}
